<?php

namespace app\api\model\project;

use app\api\model\User;
use think\Db;
use think\Model;
use traits\model\SoftDelete;


class ProjectStaff extends Model
{


    use SoftDelete;


    // 表名
    protected $name = 'project_staff';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'gender_text'
    ];

    public function getGenderList()
    {
        return [0=>'女',1=>'男'];
    }

    public function getGenderTextAttr($value, $data)
    {
        $value = $value ?: ($data['gender'] ?? '');
        $list = $this->getGenderList();
        return $list[$value] ?? '';
    }

    //发布服务
    public function apply($params)
    {
        Db::startTrans();
        try {
            $model = new ProjectStaff();
            $cert_img = json_decode($params['cert_img'],true)?json_decode($params['cert_img'],true):_json_decode($params['cert_img']);
            $service_img = json_decode($params['service_img'],true)?json_decode($params['service_img'],true):_json_decode($params['service_img']);
            $service_video = json_decode($params['service_video'],true)?json_decode($params['service_video'],true):_json_decode($params['service_video']);
            $service_project = json_decode($params['service_project'],true)?json_decode($params['service_project'],true):_json_decode($params['service_project']);

            //dump($service_project);die;
            $data = [
                'user_id'=>$params['user_id'],
                'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],
                'mobile'=>$params['mobile'],
                'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],
                'name'=>$params['name'],
                'region_limit'=>$params['region_limit'],
                //'user_level'=>isset($params['user_level'])?','.$params['user_level'].',':'',
                'user_level'=>isset($params['user_level'])?$params['user_level']:'',
                'self_intro'=>isset($params['self_intro'])?$params['self_intro']:'',
                'place_province'=>isset($params['place_province'])?$params['place_province']:'',
                'place_city'=>isset($params['place_city'])?$params['place_city']:'',
                'place_district'=>isset($params['place_district'])?$params['place_district']:'',
                'place_province_id'=>isset($params['place_province_id'])?$params['place_province_id']:'',
                'place_city_id'=>isset($params['place_city_id'])?$params['place_city_id']:'',
                'place_district_id'=>isset($params['place_district_id'])?$params['place_district_id']:'',
            ];

            $count = ProjectStaff::where(['user_id'=>$data['user_id']])->count();

            $user_id = $data['user_id'];
            $user = User::get($user_id);

            if ($user['level']==0){
                throw new \Exception('普通用户不能操作');
            }
            if ($user_id!=37&&$user_id!=164){
                if ($count>=5){
                    throw new \Exception('每个会员最多申请5次服务');
                }
            }

            if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }
            if (empty($data['realname'])){
                throw new \Exception('请输入姓名');
            }
            if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }

            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            /*if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/
            if (empty($service_project)){
                throw new \Exception('请选择服务项目');
            }
            if (empty($data['image'])){
                throw new \Exception('请上传标准照');
            }
            /*if (empty($cert_img)){
                throw new \Exception('请上传证书');
            }*/
            /*if (empty($service_img)){
                throw new \Exception('请上传服务图片');
            }*/
            /*if (empty($service_video)){
                throw new \Exception('请上传服务视频');
            }*/
            $res = $model->save($data);
            if ($res){
                $media_model = new ProjectMedia();
                $media_list = [];
                foreach ($cert_img as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$model->id,
                        'type'=>0,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_img as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$model->id,
                        'type'=>1,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_video as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$model->id,
                        'type'=>2,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                $media_model->saveAll($media_list);

                //服务项目
                $apply_model = new ProjectApply();

                $service_project_list = [];
                foreach ($service_project as $k=>$v){
                    foreach ($v['project'] as $kk=>$vv){
                        $service_project_list[] = [
                            'project_id'=>$vv['id'],
                            'staff_id'=>$model->id,
                            'category_id'=>$vv['category_id'],
                            'price'=>$vv['price'],
                            'name'=>$vv['name'],
                            'unit'=>$vv['unit'],
                            'remark'=>$vv['remark'],
                            'work_day'=>$v['work_day'],
                            'service_start_time'=>$v['service_start_time'],
                            'service_end_time'=>$v['service_end_time'],
                            'explain'=>$v['explain'],
                        ];
                    }

                }

                $apply_model->saveAll($service_project_list);
            }
            Db::commit();
            return $res;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }



    //修改
    public function edit($params)
    {
        $staff_id = $params['staff_id'];
        Db::startTrans();
        try {
            $model = new ProjectStaff();
            $cert_img = json_decode($params['cert_img'],true)?json_decode($params['cert_img'],true):_json_decode($params['cert_img']);
            $service_img = json_decode($params['service_img'],true)?json_decode($params['service_img'],true):_json_decode($params['service_img']);
            $service_video = json_decode($params['service_video'],true)?json_decode($params['service_video'],true):_json_decode($params['service_video']);
            $service_project = json_decode($params['service_project'],true)?json_decode($params['service_project'],true):_json_decode($params['service_project']);

            //dump($service_project);die;
            $data = [
                'nickname'=>$params['nickname'],
                'realname'=>$params['realname'],
                'gender'=>$params['gender'],
                'age'=>$params['age'],
                'education'=>$params['education'],
                'major'=>$params['major'],
                'title'=>$params['title'],
                'mobile'=>$params['mobile'],
                'image'=>$params['image'],
                'province'=>$params['province'],
                'city'=>$params['city'],
                'district'=>$params['district'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'district_id'=>$params['district_id'],
                'name'=>$params['name'],
                'region_limit'=>$params['region_limit'],
                'user_level'=>isset($params['user_level'])?$params['user_level']:'',

                'self_intro'=>isset($params['self_intro'])?$params['self_intro']:'',
                'place_province'=>isset($params['place_province'])?$params['place_province']:'',
                'place_city'=>isset($params['place_city'])?$params['place_city']:'',
                'place_district'=>isset($params['place_district'])?$params['place_district']:'',
                'place_province_id'=>isset($params['place_province_id'])?$params['place_province_id']:'',
                'place_city_id'=>isset($params['place_city_id'])?$params['place_city_id']:'',
                'place_district_id'=>isset($params['place_district_id'])?$params['place_district_id']:'',
            ];

            /*$user_id = $data['user_id'];
            $user = User::get($user_id);

            if ($user['level']==0){
                throw new \Exception('普通用户不能操作');
            }*/


            if (empty($data['nickname'])){
                throw new \Exception('请输入昵称');
            }
            if (empty($data['realname'])){
                throw new \Exception('请输入姓名');
            }
            if (empty($data['age'])){
                throw new \Exception('请输入年龄');
            }
            if (empty($data['education'])){
                throw new \Exception('请输入学历');
            }
            if (empty($data['major'])){
                throw new \Exception('请输入专业');
            }
            if (empty($data['title'])){
                throw new \Exception('请输入职称');
            }

            if (empty($data['mobile'])){
                throw new \Exception('请输入电话');
            }
            /*if (empty($data['province'])||empty($data['city'])||empty($data['district'])){
                throw new \Exception('请选择区域');
            }*/
            if (empty($service_project)){
                throw new \Exception('请选择服务项目');
            }
            if (empty($data['image'])){
                throw new \Exception('请上传标准照');
            }
            /*if (empty($cert_img)){
                throw new \Exception('请上传证书');
            }*/
            /*if (empty($service_img)){
                throw new \Exception('请上传服务图片');
            }*/
            /*if (empty($service_video)){
                throw new \Exception('请上传服务视频');
            }*/
            $res = $model->save($data,['id'=>$staff_id]);
            if ($res){
                ProjectMedia::where(['staff_id'=>$staff_id])->delete();
                $media_model = new ProjectMedia();
                $media_list = [];
                foreach ($cert_img as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$staff_id,
                        'type'=>0,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_img as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$staff_id,
                        'type'=>1,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                foreach ($service_video as $k=>$v){
                    $media_list[] = [
                        'staff_id'=>$staff_id,
                        'type'=>2,
                        'media'=>$v['media'],
                        'title'=>$v['title'],
                        'describe'=>$v['describe']
                    ];
                }
                $media_model->saveAll($media_list);

                //服务项目
                //$apply_model = new ProjectApply();

                //$service_project_list = [];
                foreach ($service_project as $k=>$v){
                    foreach ($v['project'] as $kk=>$vv){
                        $info = ProjectApply::get(['staff_id'=>$staff_id]);
                        if ($info){
                            $info->price = $vv['price'];
                            $info->setAttr('name',$vv['name']);
                            $info->unit = $vv['unit'];
                            $info->remark = $vv['remark'];
                            $info->work_day = $v['work_day'];
                            $info->service_start_time = $v['service_start_time'];
                            $info->service_end_time = $v['service_end_time'];
                            $info->explain = $v['explain'];
                            $info->save();
                        }

                    }
                }
                /*if (count($service_project_list)>0){
                    $apply_model->saveAll($service_project_list);
                }*/

            }
            Db::commit();
            return $res;
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new \Exception($e->getMessage());
        }

    }


}
